package android.slc.fast.vd.list

import cn.hutool.core.collection.CollectionUtil
import javafx.application.Platform
import javafx.scene.control.ScrollPane
import javafx.scene.layout.GridPane
import org.apache.commons.lang3.StringUtils
import java.io.Serializable

/**
 * 基础的单次加载盒子vm
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
abstract class BaseGridViewVd<T : Serializable, VS : BaseListVmSub<T>> : BaseListVd<T, VS>(), BaseGridViewVdIface<T> {

    protected lateinit var scrollPane: ScrollPane
        private set

    private lateinit var gridPane: GridPane

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        if (StringUtils.isEmpty(getScrollPaneId())) {
            this.gridPane = contentView.lookup(getGridPaneId()) as GridPane
        } else {
            this.scrollPane = contentView.lookup(getScrollPaneId()) as ScrollPane
            this.gridPane = scrollPane.content.lookup(getGridPaneId()) as GridPane
        }
    }

    override fun getGridPane(): GridPane {
        return gridPane
    }

    override fun notifyRecyclerRefresh(list: List<T>) {
        super.notifyRecyclerRefresh(list)
        Platform.runLater {
            if (CollectionUtil.isEmpty(list)) {
                //清除
                gridPane.children.clear()
            } else {
                //填充
                loadItemBefore(list)
                loadItem(list)
            }
        }
    }

}
